package com.hm.pan.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpURLUtil {

	/**
	 * 剥除URL中的查询参数(QueryString)
	 */
	public static String stripQueryString(String url) {
		if (StringUtil.isEmpty(url)) {
			return null;
		}

		int indexOf = url.indexOf('?');

		if (indexOf == -1) {
			return url;
		}

		String substring = url.substring(0, indexOf);

		return StringUtil.isEmpty(substring) ? null : substring;
	}

	/**
	 * 发送GET请求
	 */
	public static byte[] doGet(String url) {
		InputStream is = null;

		try {
			HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
			conn.setRequestMethod("GET");
			conn.setRequestProperty("User-Agent", "QianDuCloudisk 1.0");
			is = conn.getInputStream();

			byte[] buffer = new byte[4096];
			int length;

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((length = is.read(buffer)) != -1) {
				baos.write(buffer, 0, length);
			}

			baos.close();
			return baos.toByteArray();
		} catch (Exception e) {
			return null;
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
				}
			}
		}
	}

}
